<?php
namespace app\controller;

use app\Controller;
use app\service\Wechat as Service;

/*
 * 公众号控制器
 * @author Mateng <Index.php>
 */
class Wechat extends Controller
{

    // setOpenid
    public function setOpenid () {
        $appid = config('wechat.appid');
        if(session('?' . $appid . '_openid')){
            return json(['msg' => '未成功获取授权', 'code' => 206]);
        }
        $oauth = new Service();
        if (($token = $oauth->getOauthAccessToken()) && isset($token['openid'])) {
            session($appid . '_openid', $token['openid']);
            return json($result);
        } else {
            return json(['msg' => '未成功获取授权', 'code' => 205]);
        }
    }
    // getOpenid
    public function getOpenid () {
        $appid = config('wechat.appid');
        if(session('?' . $appid . '_openid')){
            return json(session($appid . '_openid'));
        }
        return json(['msg' => '', 'code' => 261]);
    }
}
